package com.sgb.kjwl.view.ui.activity.self.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.ActivityUserOfferBinding;
import com.sgb.kjwl.model.entity.TenderDetailEntity;
import com.sgb.kjwl.model.entity.user.RotationEntity;
import com.sgb.kjwl.view.ui.fragment.self.pick.UserOfferFragment;
import com.sgb.kjwl.viewmodel.self.pick.UserOfferModel;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;

import javax.inject.Inject;

public class UserOfferActivity extends AppActivity {
    private ActivityUserOfferBinding binding;
    @Inject
    UserOfferModel viewModel;

    private String[] titles ;
    private SlidingFragmentPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String id;

    public static void start(Context context, TenderDetailEntity tenderDetailEntity) {
        Intent intent = new Intent();
        intent.putExtra("tenderDetailEntity", GsonManager.getInstance().getGson().toJson(tenderDetailEntity));
        intent.setClass(context, UserOfferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        String tenderDetailEntity = getIntent().getStringExtra("tenderDetailEntity");
        TenderDetailEntity entity = GsonManager.getInstance().getGson().fromJson(tenderDetailEntity, TenderDetailEntity.class);

        binding = DataBindingUtil.setContentView(UserOfferActivity.this, R.layout.activity_user_offer);
        binding.setViewModel(viewModel);
        binding.include.titleView.setText("报价");
        binding.include.ivBack.setOnClickListener(this);
        ArrayList<String> names = new ArrayList<>();
        for (RotationEntity rotationEntity : entity.getRotationList()){
            names.add(rotationEntity.getRotationStr());
            mFragments.add(UserOfferFragment.newInstance(rotationEntity.getId()));
        }
        titles = (String[])names.toArray(new String[names.size()]);
        pagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(names.size());
        binding.tabLayout.setupWithViewPager(binding.viewpager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
